package com.running.base.run.util;

import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static String getCurrentDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    public static String getCurrentDateTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static String getCurrentDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());
    }

    public static double getDaysBetweenDay2Day(Date start, Date end) {
        double remain_days = 0;
        long difference = end.getTime() - start.getTime();
        double hours = difference / (1000 * 60 * 60);

        int days = (int) (hours / 24);
        if  (days >= 1) {
            int re_hour = (int) (hours - days * 24);
            if (re_hour <= 5) {
                remain_days = (double) (days + 0.5);
            } else {
                remain_days = (double) (days + 1);
            }
        }
        return remain_days;
    }

    public static int getCurrentMonthDays() {
        Calendar calendar = Calendar.getInstance();
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public static Date getCurrentMonthDate() throws ParseException {
        // 获取当前年月
        YearMonth yearMonth = YearMonth.now();
        // 获取当前月份的最后一天
        LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();
        return parseDateTime(lastDayOfMonth.toString());
    }

    public static Date parseDateTime(String date) throws ParseException {
        StringBuilder addDate = new StringBuilder();
        addDate.append(date).append(" 23:59:59");
        return DateUtils.parseDate(addDate.toString(), "yyyy-MM-dd HH:mm:ss");
    }

    public static Date parseDateTimeLess(String date) throws ParseException {
        StringBuilder addDate = new StringBuilder();
        addDate.append(date).append(" 00:00:00");
        return DateUtils.parseDate(addDate.toString(), "yyyy-MM-dd HH:mm:ss");
    }

    public static LocalDateTime dateToLocalDateTime(Date date) {
        // 将Date对象转换为Instant对象
        Instant instant = date.toInstant();

        // 将Instant对象转换为ZonedDateTime对象
        ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());

        // 将ZonedDateTime对象转换为LocalDateTime对象
        LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();

        return localDateTime;
    }

    public static Date parseStrToDate(String date) throws ParseException {
        return DateUtils.parseDate(date, "yyyy-MM-dd HH:mm:ss");
    }


    public static Date parseToDateT(String dateString) throws ParseException {
        if (StringUtil.isNull(dateString)) return null;
        Date parseDate = DateUtils.parseDate(dateString, "yyyy-MM-dd'T'HH:mm");
        return parseDate;
    }
}
